package main

import (
	"flag"
	"io"
	"log"

	"os"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/widget"
)

var data = [][]string{[]string{"", "", ""}, []string{"", "", ""}}
var status *widget.Label
var idxSelected string
var myWindow fyne.Window
var table *widget.Table
var loc fyne.ListableURI
var filename string
var password string
var statusInit string

func main() {
	flag.Parse()
	if flag.Arg(0) != "" {
		statusInit = flag.Arg(0)
		openZip(flag.Arg(0))
	}

	myApp := app.New()
	myApp.Settings().SetTheme(GetMyTheme())
	myWindow = myApp.NewWindow("Unzip Go GUI")

	bar := createToolBar()
	list := createTable()
	table = list
	list.OnSelected = func(id widget.TableCellID) {
		idxSelected = data[id.Row][0]
	}
	list.SetColumnWidth(0, 50)
	list.SetColumnWidth(1, 80)
	list.SetColumnWidth(2, 660)

	scroll := container.NewHScroll(list)
	status = widget.NewLabel(statusInit)
	content := container.NewBorder(bar, status, nil, nil, scroll)

	myWindow.SetContent(content)
	myWindow.Resize(fyne.NewSize(800, 600))
	myWindow.ShowAndRun()

	os.Unsetenv("FYNE_FONT")
}

func createToolBar() *fyne.Container {
	btOpen := widget.NewButton("打开", func() {
		filter := storage.NewExtensionFileFilter([]string{".zip", ".ZIP", ".Zip"})
		dlg := dialog.NewFileOpen(func(r fyne.URIReadCloser, e error) {
			//log.Println(r)
			if e == nil && r != nil {
				p := r.URI().Path()
				r.Close()
				table.Hide()
				err := openZip(p)
				if err == io.EOF {
					table.Refresh()
					table.Show()
					status.SetText("打开文件：" + p)
				} else {
					status.SetText(err.Error())
				}

			} else {
				if e != nil {
					status.SetText(e.Error())
				}

			}
		}, myWindow)
		dlg.SetFilter(filter)
		if loc != nil {
			dlg.SetLocation(loc)
		}
		dlg.Show()
	})
	btAll := widget.NewButton("全部解压", func() {
		unpackZip(filename)
	})
	btSelected := widget.NewButton("解压选中", func() {
		if len(idxSelected) > 0 {
			pickSelected(idxSelected, filename)
		}

	})

	btAllTo := widget.NewButton("全部解压到...", func() {
		dlg := dialog.NewFolderOpen(
			func(dir fyne.ListableURI, e error) {
				if e != nil {
					log.Println(e.Error())
					return
				}
				if dir != nil {
					unpackZipTo(filename, dir.Path())
				}

			},
			myWindow)
		dlg.SetLocation(loc)
		dlg.Show()
		return
	})

	btSelectedTo := widget.NewButton("解压选中到...", func() {
		if len(idxSelected) == 0 {
			return
		}
		dlg := dialog.NewFolderOpen(
			func(dir fyne.ListableURI, e error) {
				if e != nil {
					log.Println(e.Error())
					return
				}
				if dir != nil {
					pickSelectedTo(idxSelected, filename, dir.Path())
				}

			},
			myWindow)
		dlg.SetLocation(loc)
		dlg.Show()
		return
	})

	box := container.NewHBox(btOpen, btAll, btSelected, btAllTo, btSelectedTo)
	return box
}

func createTable() *widget.Table {
	list := widget.NewTable(
		func() (int, int) {
			//获取行数和列数
			return len(data), len(data[0])
		},
		func() fyne.CanvasObject {
			return widget.NewLabel("content")
		},
		func(i widget.TableCellID, o fyne.CanvasObject) {
			o.(*widget.Label).SetText(data[i.Row][i.Col])
		},
	)

	return list
}
